package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(path = "/user")
public class UserController {

    @Reference
    private UserService userService;

    /**
     * 获取当前登录用户的用户名
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/getUsername")
    public Result getUsername(HttpServletResponse response) throws Exception {
        try {
            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            //登录成功
            //写入Cookie，跟踪用户
            Cookie cookie = new Cookie("login_name",userDetails.getUsername());
            cookie.setPath("/");//路径
            cookie.setMaxAge(60*60*24*30);//有效期30天
            response.addCookie(cookie);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, userDetails.getUsername());
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
    @PostMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds){
        try{
            userService.add(user,roleIds);
            return new Result(true,MessageConstant.ADD_USER_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
    }
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        try{
            PageResult pageResult = userService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,pageResult);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAILED);
        }
    }
    @GetMapping("/findById")
    public Result findById(Integer id){
        try{
            User user = userService.findById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAILED);
        }
    }
    @GetMapping("/{id}/roleIds")
    public Result findRoleIdsAssociationIds(@PathVariable("id") Integer userId){
        try{
            Integer[] roleIds = userService.findRoleIdsAssociationIds(userId);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleIds);
        }catch(Exception e){
            return new Result(false,MessageConstant.QUERY_ROLE_FAILED);
        }
    }
    @PutMapping("/update")
    public Result update(@RequestBody User user,Integer[] roleIds){
        try{
            userService.update(user,roleIds);
            return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAILED);
        }
    }
    @PreAuthorize("hasAuthority('USER_DELETE')")
    @DeleteMapping("/delete")
    public Result delete(Integer userId){
        try{
            userService.delete(userId);
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.DELETE_USER_FAILED);
        }
    }
}
